package api

import (
	"fmt"
	"runtime/debug"

	"github.com/deploy-services/v2/KolonseWeb"
	"github.com/deploy-services/v2/KolonseWeb/HttpLib"
	"github.com/deploy-services/v2/KolonseWeb/Type"
	"github.com/deploy-services/v2/services"
)

func daemon_router(app *KolonseWeb.App) {
	// // ?name=xxx
	app.Get("/api/daemon/start", daemon_start)
	app.Get("/api/daemon/stop", daemon_stop)
	app.Get("/api/daemon/status", daemon_status)
}

func daemon_start(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.DaemonStart(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}

func daemon_stop(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.DaemonStop(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}

func daemon_status(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.DaemonStatus(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}
